// Fill out your copyright notice in the Description page of Project Settings.


#include "ScreenshotViewport.h"


/**
 * FStoneDummyViewport
 */
static const FName NAME_STDummyViewport = FName(TEXT("STDummyViewport"));

FStoneDummyViewport::FStoneDummyViewport(FViewportClient* InViewportClient)
	: FViewport(InViewportClient)
	, DebugCanvas(NULL)
{
	ViewportType = NAME_STDummyViewport;

}

FStoneDummyViewport::~FStoneDummyViewport()
{
	if (DebugCanvas != NULL)
	{
		delete DebugCanvas;
		DebugCanvas = NULL;
	}
}

void FStoneDummyViewport::InitRHI(FRHICommandListBase&)
{
	EPixelFormat DummyViewportFormat = bSceneHDREnabled ? GRHIHDRDisplayOutputFormat : PF_A2B10G10R10;
	const FRHITextureCreateDesc Desc =
		FRHITextureCreateDesc::Create2D(TEXT("FStoneDummyViewport"))
		.SetExtent(SizeX, SizeY)
		.SetFormat(DummyViewportFormat)
		.SetFlags(ETextureCreateFlags::RenderTargetable | ETextureCreateFlags::ShaderResource)
		.SetInitialState(ERHIAccess::SRVMask);

	RenderTargetTextureRHI = RHICreateTexture(Desc);
}

EDisplayColorGamut FStoneDummyViewport::GetDisplayColorGamut() const
{
	return DisplayColorGamut;
}

EDisplayOutputFormat FStoneDummyViewport::GetDisplayOutputFormat() const
{
	return DisplayOutputFormat;
}

bool FStoneDummyViewport::GetSceneHDREnabled() const
{
	return bSceneHDREnabled;
}


void FStoneDummyViewport::EndFrame(FStoneDummyViewport* DummyViewport)
{
	ENQUEUE_RENDER_COMMAND(EndFrameCommand)(
		[DummyViewport](FRHICommandListImmediate& RHICmdList)
		{
			if (DummyViewport)
			{
				DummyViewport->EndRenderFrame(RHICmdList, false, false);
			}
			FCoreDelegates::OnEndFrameRT.Broadcast();
			RHICmdList.EndFrame();
			GPU_STATS_ENDFRAME(RHICmdList);
		});
}

void FStoneDummyViewport::BeginFrame(FStoneDummyViewport* DummyViewport)
{
	GFrameCounter++;
	ENQUEUE_RENDER_COMMAND(BeginFrameCommand)(
		[DummyViewport, CurrentFrameCounter = GFrameCounter](FRHICommandListImmediate& RHICmdList)
		{
			GFrameCounterRenderThread = CurrentFrameCounter;
			GFrameNumberRenderThread++;
			GPU_STATS_BEGINFRAME(RHICmdList);
			RHICmdList.BeginFrame();
			FCoreDelegates::OnBeginFrameRT.Broadcast();
			if (DummyViewport)
			{
				DummyViewport->BeginRenderFrame(RHICmdList);
			}
		});
}